import { InnerMineEntity, MinerialEntity, SpawnEntity } from "GameLoop/WorldState/EntityManager/Entitys";
import { State } from "framework/FSM/AState";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";

import { ActionUtils } from "../utils/ActionUtil";


/**
 * 搬运工行为。搬运矿物的任务
 */ 
export class PorterMinerialTask<T extends IStateArg<Creep>>  extends State<T>
{
    /**
     * 搬运工行为,先做简化行为。运输的两端都有container
     * @param arg 
     */
    public  getActions(arg:T):Action[]
    {
        // 2.
         
        const creep = arg.entity;
        if(!creep.memory.taskid)
        {

            return []
        }
       
        const task =arg.task as ITask<ICreepTaskData<"harvestM">>;
        if(!task)
        {
            return []
        }
        
        // 搬运工
        const bindMine = arg.entity.getEntityByID<MinerialEntity>(task.data.arg.entityId);
        if(!bindMine.room)
        {
            // 房间不在视野
            return [ActionArgHelper.actionMoveto(creep,{pos:bindMine.getWorkPosition(),range:1},{})]
        }
        const actions = ActionUtils.getEscapeSourceKeeperAction(creep,bindMine.BasePosition);
        if(actions)
        {
            
            return actions
        }
        let isStateChange = 0;
        if(creep.store.getUsedCapacity()==0 )
        {
            if(creep.memory.state==1)
            {
                isStateChange=-1;
            }
            creep.memory.state = 0;
        }
        else  if(creep.store.getFreeCapacity()==0|| creep.ticksToLive!<task.data.cost*2)
        {
            if(creep.memory.state==0)
            {
                isStateChange= 1;
            }
            creep.memory.state = 1;
            // 绝不空载
        }
        if(isStateChange == 1 ||!creep.memory.targetId)
        {
            // 状态改变
            const coreEntity = creep.ownerRoom().getEntity("CoreEntity");
            const stores = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:creep.store.getCapacity()});
            
            if(stores.length>0)
            {
                creep.memory.targetId = stores[0].id;
            }
            else 
            {
                creep.log('info',`没有可以存de仓库`);
            }
        }
        
        if(creep.memory.state)
        {
            if(creep.memory.targetId)
            {
                const store = Game.getObjectById(creep.memory.targetId) as StructureTerminal|StructureStorage;
                if(store && store.store.getFreeCapacity()>0)
                {
                    return [ActionArgHelper.actionTransfer(creep,store)];
                }
                creep.log('info',`targetid`,creep.memory.targetId,'重新清空');
                creep.memory.targetId=undefined;
            }
             
            else 
            {
                creep.log('info',`没有可以存资源得仓库`);
            }
        }
        else
        {
            
            // 取资源从其他爬身上取
            if(creep.name == task.data.creeps[0])
            {
                 // 只有排在第一，的爬能够取资源
                const harvester = task.data.creeps.map(e=>Game.creeps[e]).filter(e=>e && e.ActiveRole() == "minex" && e.store.getUsedCapacity()>0 );
                if(harvester.length>0)
                {  
                    if(harvester[0].pos.inRangeTo(creep.pos,1))
                    {
                        return [ActionArgHelper.actionTransfer(harvester[0],creep)];
                    }
                    else 
                    {
                        return [ActionArgHelper.actionMoveto(creep,{pos:harvester[0].pos,range:1},{})]
                    }
                }
            }
            
            // 获取从地上拾取
            
            const pickitem = bindMine.getPickUpItem(bindMine.MinerialType);
            if(pickitem.length)
            {
                 
                return  [
                    ActionArgHelper.warp(
                        ActionArgHelper.actionPickup(creep,pickitem[0]),
                        undefined,
                        (value )=>
                        {
                           return value.arg.entity.ticksToLive!<task.data.cost
                        })]
            }
            else 
            {
                const container = bindMine.getContainer();
                if(container && container.store.getUsedCapacity()>0)
                {
                    return [ActionArgHelper.warp(
                        ActionArgHelper.actionWithdraw(creep,container),
                        undefined,
                        (value )=>
                        {
                            return value.arg.entity.ticksToLive!<task.data.cost
                        })]
                }
                return [ActionArgHelper.warp(
                    ActionArgHelper.actionMoveto(creep,{pos:bindMine.BasePosition,range:2},{IsEludeHostile:creep.room.getType() ==6 ?true:false}),
                    undefined,
                    (value )=>
                    {
                        return value.arg.entity.store.getFreeCapacity()<value.arg.entity.store.getCapacity()*0.2
                        // if( value.entity.ticksToLive!<task.data.cost)
                        // {
                        //     return true;
                        // }
                        
                    }
                    )]
            }
            
        }
        creep.log('info',`没有确定得行为`)
        return [ ];
    }
}
